<?php
// +----------------------------------------------------------------------
// | yylAdmin 前后分离，简单轻量，免费开源，开箱即用，极简后台管理系统
// +----------------------------------------------------------------------
// | Copyright https://gitee.com/skyselang All rights reserved
// +----------------------------------------------------------------------
// | Gitee: https://gitee.com/skyselang/yylAdmin
// +----------------------------------------------------------------------

namespace app\admin\library\account;

// use think\facade\Config;
use think\Validate;

use app\common\model\MenuModel;
use app\common\model\Account;
use think\Cache;
/**
 * 用户管理
 */
class UserService
{
    // 缓存标签
    public static $tag = 'system_user';
    // 缓存前缀
    protected static $prefix = 'system_user:';

    /**
     * 用户信息
     *
     * @param int  $id   用户id
     * @param bool $exce 不存在是否抛出异常
     * @param bool $role 是否返回角色信息
     * 
     * @return array|Exception
     */
    public static function info($id, $exce = true, $role = false)
    {
        // $info = Cache::get(self::$prefix.$id);
        if (empty($info)) {
            $model = new Account();

            $info = $model->with('roles')->find($id);
            if (empty($info)) {
                if ($exce) {
                    throw new \Exception('用户不存在：' . $id);
                }
                return [];
            }
            $info = $info
                ->append(['role_ids'])
                ->toArray();
            $MenuModel = new MenuModel();
            $MenuPk = $MenuModel->getPk();
            // var_dump($info['role_id']);die;
            // 超级管理员
            if ($info['role_id'] == 0) {
                $menu      = $MenuModel->field($MenuPk . ',menu_url')->where('is_delete',0)->select()->toArray();
                $menu_ids  = array_column($menu, 'menu_id');
                $menu_urls = array_filter(array_column($menu, 'menu_url'));
            } else {
                $role_menu_ids   = RoleService::menu_ids($info['role_ids'], where_disdel());
                
                $unauth_menu_ids = MenuService::unauthList('id');
                
                $menu_ids        = array_merge($role_menu_ids, $unauth_menu_ids);
                
                $menu_urls       = $MenuModel->where(['menu_id' => ['in',$menu_ids]])->where('is_delete',0)->column('menu_url');
                $menu_urls       = array_filter($menu_urls);
                
            }
            if (empty($menu_ids)) {
                $menu_ids = [];
            } else {
                foreach ($menu_ids as $k => $v) {
                    $menu_ids[$k] = (int) $v;
                }
            }
            
            // $menu_is_unlogin = Config::get('admin.menu_is_unlogin', []);
            // $menu_is_unauth  = Config::get('admin.menu_is_unauth', []);
            // $unlogin_unauth  = array_merge($menu_is_unlogin, $menu_is_unauth);
            // $menu_urls       = array_unique(array_merge($menu_urls, $unlogin_unauth));
            // var_dump($menu_ids);die;
            $info['roles']    = array_values($menu_urls);
            $info['menus']    = MenuService::menus($menu_ids);
            $info['menu_ids'] = $menu_ids;
            
            // Cache::tag(self::$tag)->set(self::$prefix.$id, $info, 43200);
        }
        
        if ($role) {
            $user_menu_ids = $info['menu_ids'] ?? [];
            $role_menu_ids = RoleService::menu_ids($info['role_ids'], where_disdel());
            $menu_list = MenuService::list('list', where_delete(), [], 'menu_id,menu_pid,menu_name,menu_url,is_unlogin,is_unauth');
            
            foreach ($menu_list as &$val) {
                $val['is_check'] = 0;
                $val['is_role'] = 0;
                foreach ($user_menu_ids as $m_menu_id) {
                    if ($val['menu_id'] == $m_menu_id) {
                        $val['is_check'] = 1;
                    }
                }
                foreach ($role_menu_ids as $g_menu_id) {
                    if ($val['menu_id'] == $g_menu_id) {
                        $val['is_role'] = 1;
                    }
                }
            }
            $info['menu_tree'] = list_to_tree($menu_list, 'menu_id', 'menu_pid');
        }
        return $info;
    }
}
